<?php
//ienerating $content for admin / articles.php
//articles list
if(!$_GET['cmd'] or $_GET['cmd']=='suggest') {
	if(!perms_check('article', 'edit') and !perms_check('article', 'del')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	SQLvalidate($_GET['rec']);
	if(!$_GET['rec'])
		$_GET['rec']=0;

	if(!$_GET['cat'])
		$_GET['cat']=0;

	$fields=array('id', 'title', 'text', 'visible', 'cat', 'author', 'date');

	//sort
	if($_GET['sort']!='ASC' && $_GET['sort'] != 'DESC')
		$_GET['sort']='DESC';
	if(!$_GET['sortby'] or !in_array($_GET['sortby'], $fields))
		$_GET['sortby']='id';
	if($_GET['sort']=='ASC')
		$sortnext='DESC';
	else
		$sortnext='ASC';
	//

	if($_GET['sortby'] == 'title' or $_GET['sortby'] == 'text')
		$_GET['sortby'] .= "_$_SESSION[lang_short]";

	$content = string_template(read_file('admin/themes/articles_header.php'), array("sortnext" => $sortnext));

	//search
	if($_GET['search'])
		$_POST['search']=$_GET['search'];
	$_POST['search']=read_text_rest($_POST['search']);
	if($_POST['search']){
		$src=str_replace('*', '%', $_POST['search']);
		$sql=" WHERE title_$_SESSION[lang_short] LIKE '%$src%' or text_$_SESSION[lang_short] LIKE '%$src%'";
	}
	else
		$sql='';

	//suggest
	if($sql && $_GET['cmd']=='suggest')
		$sql.=' && visible=0';
	elseif($_GET['cmd']=='suggest')
		$sql.=' WHERE visible=0';

	//scategory (and subcategories)
	if($sql && $_GET['cat'])
		$sql.=' && cat IN ('.implode(", ", get_subcategories($_GET['cat'])).', '.$_GET['cat'].')';
				elseif($_GET['cat'])
				$sql.=' WHERE cat IN ('.implode(", ", get_subcategories($_GET['cat'])).', '.$_GET['cat'].')';	
					$db = new dbquery;

					$db->query("SELECT * FROM $conf[prefix]article$sql") or $db->err(__FILE__, __LINE__);
					$q=$db->num_rows();

					$db->query("SELECT * FROM $conf[prefix]article$sql ORDER BY ".$_GET['sortby']." $_GET[sort] LIMIT $_GET[rec], $conf[admin_per_page]") or $db->err(__FILE__, __LINE__); 
					while($art = $db->fetch_object()) {
					$i++;
					if($i>2)
					$i=1;

					$tlt = "title_$_SESSION[lang_short]";

					if($art->$tlt == '') {
					$tlt = "title_".$conf['language'];
					}

					$title = $art->$tlt;
					$cat=get_cat_by_id($art->cat);

					$content .= string_template(read_file('admin/themes/articles_item.php'), array("id" => $art->id, "title" => add_dots(read_text_rest($title), 30), "cat" => get_cat_name_by_id($art->cat), 'cat_id'=>$art->cat, "date" => date($conf['date_format'], strtotime($art->date)), "visible" => $art->visible, "i" => $i));
					}

					$page_link=split_to_pages('<A HREF="index.php?module=admin&amp;action=articles&amp;sortby='.$_GET['sortby'].'&amp;sort='.$_GET['sort'].'&search='.$_POST['search'].'&amp;cmd='.$_GET['cmd'].'&amp;rec={rec}">{nr}</A>', $conf['admin_per_page'], $q, $_GET['rec']);

					$content .= string_template(read_file('admin/themes/articles_footer.php'), array('page_link' => $page_link));
}
elseif($_GET['cmd']=='edit')
{
	if(!perms_check('article', 'edit'))
	{
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	SQLvalidate($_GET['id']);

	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix]categories");

	//generating list of categories
	$categories = '<option value="no_category">no_category</option>';
	//

	$db->query("SELECT * FROM $conf[prefix]article WHERE id=$_GET[id]") or $db->err(__FILE__, __LINE__);

	//sprawdzanie czy istnieje
	if($db->num_rows()==0) {
		redirect('index.php?module=admin&action=articles');
		exit;
	}
	//

	$d=$db->fetch_object();

	//select author
	$select_author=get_redactor_options($d->author);
	//

	if(($d->visible) == 1)
		$visible = 'checked';
	else
		$visible = '';

	$ct = NULL;
	list_categories(0, 0, $d->cat, 1, $ct, 0);

	$array = array('theme_path'=>$GLOBALS['theme_path'], 'id' => $d->id, 'cat' => $d->cat, 'function' => 'article_edit', 'c_date' => $d->date, 'c_author' => $select_author, 'c_category' => $ct, 'submit' => $lang['admin_edit'], 'checked' => $visible, "ldate" => date($conf['date_format'], strtotime($d->date)));

	$multi = NULL;
	$langs = get_lang_list();

	$i = 0;
	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$t = "title_$l";
		$array["c_title_$l"] = read_text_edit($d->$t);
		$t = "text_$l";
		$array["c_text_$l"] = read_text_edit($d->$t);
		$multi .= string_template(read_file('admin/themes/articles_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));
	}

	$content = string_template(read_file('admin/themes/articles_form.php'), array('multi' => $multi));
	$content = string_template($content, $array);
}
elseif($_GET['cmd'] == 'delete') {
	if(!perms_check('article', 'del'))
	{
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	SQLvalidate($_GET['id']);

	$db = new dbquery;
	$db->query("SELECT * FROM $conf[prefix]article WHERE id=$_GET[id]");

	//sprawdzanie czy istnieje
	if($db->num_rows()==0) {
		redirect('index.php?module=admin&action=articles');
		exit;
	}
	//

	$d = $db->fetch_array();

	$yes='<input type="button" onClick="javascript:location.href=\'index.php?module=article&function=article_delete&amp;article='.$d['id'].'\'" value="'.$lang['yes'].'" class="button">';
	$no='<input type="button" onClick="javascript:location.href=\'javascript:history.go(-1);\'" value="'.$lang['no'].'" class="button">';

	$t = $d["title_$_SESSION[lang_short]"];

	$content = string_template($lang['admin_really_delete'], array('yes' => $yes, 'no' => $no, 'what' => read_text_rest($t)));      
}
elseif($_GET['cmd'] == 'add') {
	if(!perms_check('article', 'write')) {
		redirect('index.php?module=error&error=auth_error');
		exit;
	}

	$db = new dbquery;

	//select author
	$select_author=get_redactor_options($_SESSION['id']);
	//

	$ct = NULL;
	list_categories(0, 0, 'select', 1, $ct, 0);

	$array = array('theme_path'=>$GLOBALS['theme_path'], 'function' => 'article_add', 'c_date' => date('Y-m-d H:i:s'), 'c_author' => $select_author, 'c_category' => $ct, 'submit' => $lang['admin_add'], 'checked' => ' checked', 'n_selected' => ' selected', 'c_image' => '', 'checked' => ' checked', 'c_size' => '', "ldate" => date($conf['date_format'], strtotime(date('Y-m-d H:i:s'))));

	$multi = NULL;

	$langs = get_lang_list();

	$i = 0;
	foreach($langs as $key => $l) {
		$i++;
		if($l==$_SESSION['lang_short']) {
			$icon = 'collapse';
			$style = 'block';
		}
		else {
			$icon = 'expand';
			$style = 'none';
		}

		$multi .= string_template(read_file('admin/themes/articles_form_multi.php'), array('lang' => $l, 'language' => strtoupper($l), 'icon' => $GLOBALS['theme_path'].$GLOBALS['theme_img'][$icon], 'style' => $style, 'i' => $i));

		$t = "title_$l";
		$array["c_$t"] = '';
		$t = "text_$l";
		$array["c_$t"] = '';

		//recover
		if(ereg('module=error', $_SERVER['HTTP_REFERER'])) {
			$t = "title_$l";
			$array["c_$t"] = $_SESSION['recover_'.$t];
			$t = "text_$l";
			$array["c_$t"] = $_SESSION['recover_'.$t];   
		}
		//		
	}

	$content = string_template(read_file('admin/themes/articles_form.php'), array('multi' => $multi));
	$content = string_template($content, $array);
}
elseif($_GET['cmd']=='really_multi') {
	SQLvalidate($_GET['id']);
	if($_GET['id'])
		$_POST['ids']=array($_GET['id']);

	if(!is_array($_POST['ids'])) {
		redirect($_SESSION['redirect_2']);
		exit;
	}

	$ids_='';
	foreach($_POST['ids'] as $id_) {
		$ids_.='<input type="hidden" value="'.$id_.'" name="ids[]" />';
	}
	if($_GET['url'])
		$url_='<input type="hidden" value="'.urldecode($_GET['url']).'" name="url" />';

	$yes='<form action="index.php?module=article&amp;function=article_multi_'.$_GET['what'].'" method="post">'.$ids_.$url_.'<input type="submit" value="'.$lang['yes'].'" class="button" />';
	$no='<input type="button" onClick="javascript:history.go(-1);" value="'.$lang['no'].'" class="button"> </form>';

	$content=string_template($lang['admin_really'], array('yes'=>$yes, 'no'=>$no));
}

$content=string_template(read_file('middle.php'), array('theme_path' => $GLOBALS['theme_path'], 'content' => $content, 'description' => $lang['admin_articles']));
//

?>
